/**
 * Model package.
 */
package fr.umlv.m2.ig.model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;

/**
 * This class defines the lines tree model.
 * 
 * @author Gambouele Bertrand.
 * @author Viravout Alexandre.
 */
public class LinesTreeModel implements TreeModel {
  
  /**
   * The application model.
   */
  private BeauheurModelImpl model;
  
  private DefaultMutableTreeNode root;
  
  /**
   * The list of lines.
   */
  private List<Line> lines;
  
  /**
   * The list of trains.
   */
  private List<Train> trains;
  
  /**
   * Create a lines tree model object.
   * 
   * @param model the model.
   */
  public LinesTreeModel(BeauheurModelImpl model) {
    this.model = model;
    this.root = new DefaultMutableTreeNode();
    this.lines = new ArrayList<Line>();
    this.trains = model.getTrains();
    
    Iterator<Entry<String, Line>> linesIt = this.model.getLines().entrySet().iterator();
    
    Line line;
    while(linesIt.hasNext()) {
      line = linesIt.next().getValue();
      this.lines.add(line);
    }
  }

  @Override
  public Object getRoot() {
    return this.root;
  }

  @Override
  public Object getChild(Object parent, int index) {
    if(parent == this.root) {
      return "Ligne " + this.lines.get(index).getName();      
    } else if(parent instanceof String) {
      return this.trains.get(index).getId();
    }
    return null;
  }

  @Override
  public int getChildCount(Object parent) {
    if(parent == this.root) {
      return this.lines.size();      
    } else if(parent instanceof String) {
      return this.trains.size();
    }
    return 0;
  }

  @Override
  public boolean isLeaf(Object node) {

    return false;
  }

  @Override
  public void valueForPathChanged(TreePath path, Object newValue) {
    
  }

  @Override
  public int getIndexOfChild(Object parent, Object child) {

    return 0;
  }

  @Override
  public void addTreeModelListener(TreeModelListener l) {
    
  }

  @Override
  public void removeTreeModelListener(TreeModelListener l) {
    
  }
}
